package com.gnerv.sylvanas.business.manage.model.bo;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.framework.common.model.RestResultCode;
import com.gnerv.sylvanas.framework.core.exception.BusinessException;
import com.gnerv.sylvanas.business.manage.model.entity.Api;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpMethod;

import java.util.Arrays;
import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Data
public class ApiBO {

    private static final List<String> TYPE_LIST = Arrays.stream(HttpMethod.values()).map(HttpMethod::name).toList();

    private String groupUkId;
    private String ukId;
    private String name;
    private String uri;
    private String type;
    private String authority;
    private String status;
    private Integer sort;

    public Api toApi() {
        checkType();
        Api api = new Api();
        BeanUtils.copyProperties(this, api);
        api.setGmtCreate();
        api.setGmtModified();
        return api;
    }

    private void checkType() {
        if (!TYPE_LIST.contains(this.type)) {
            throw new BusinessException("type's value must be one of " + TYPE_LIST);
        }
    }
}